package com.onlinemeal.customer.controller;

import java.text.MessageFormat;
import java.util.Properties;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.onlinemeal.util.CommonUtils;
import com.onlinemeal.util.Mail;
import com.onlinemeal.util.MailUtils;
import com.onlinemeal.customer.po.Customer;
import com.onlinemeal.customer.service.CustomerService;

@Controller
public class CustomerController {
	@Autowired(required=true)
	@Resource(name="customerService")
	private CustomerService customerService;
	
	@RequestMapping("/activeCustomer")
	public String active(HttpServletRequest request,HttpServletResponse response,Model model) {
		/**
		 * 1、获取激活码
		 * 2、调用service方法完成激活
		 */
		String code = request.getParameter("code");
		customerService.active(code);
		return "show/login";
	}
	
	@RequestMapping("/registCustomer")
	public String regist(HttpServletRequest request,HttpServletResponse response,Model model) throws Exception {
		String password1 = request.getParameter("password1");
		String password2 = request.getParameter("password2");
		if(password1.equals("")) {
			model.addAttribute("message","Please input Password,Don't Empty");
			return "show/register";
		}
		if(!password1.equals(password2)) {
			/**
			 * 对于密码的判断
			 */
			model.addAttribute("message","Please input Equal Password");
			return "show/register";
		} else {
			String password = password1;
			Customer customer = CommonUtils.toBean(request.getParameterMap(),Customer.class);
			customer.setPassword(password);
			//设置唯一标示的UUID作为主键
			customer.setUid(CommonUtils.uuid());
			//设置激活码
			customer.setCode(CommonUtils.uuid());
			customer.setState("0");
			
			if(customer.getUsername().equals("")) {
				model.addAttribute("message","Please input username");
				return "show/register";
			}
			if(customer.getPhone().equals("")) {
				model.addAttribute("message","Please input phone number");
				return "show/register";
			}
			if(customer.getEmail().equals("")) {
				model.addAttribute("message","Please input email");
				return "show/register";
			}
			if(!customer.getEmail().matches("^\\s*\\w+(?:\\.{0,1}[\\w-]+)*@[a-zA-Z0-9]+(?:[-.][a-zA-Z0-9]+)*\\.[a-zA-Z]+\\s*$")) {
				model.addAttribute("message","Please input correct email");
				return "show/register";
			}
			
			System.out.println(customer);
			
			//发邮件激活
			//准备配置文件
			Properties props = new Properties();
			props.load(this.getClass().getClassLoader().getResourceAsStream("email_template.properties"));
			String host = props.getProperty("host");
			String uname = props.getProperty("uname");
			String pwd = props.getProperty("pwd");
			String from = props.getProperty("from");
			String to = customer.getEmail();
			String subject = props.getProperty("subject");
			String content = props.getProperty("content");
			content = MessageFormat.format(content, customer.getCode());
			System.out.println("===="+uname+"===="+pwd+"====");
			Session session = MailUtils.createSession(host, uname, pwd);
			Mail mail = new Mail(from,to,subject,content);
			try {
				MailUtils.send(session, mail);
			} catch (MessagingException e) {
				System.out.println("异常");
			}
			customerService.registCustomer(customer);
			model.addAttribute("message", "Success Regist ，and Login Before Please Active Your Account！");
			return "show/login";
		}
	}
	
	@RequestMapping("/loginCustomer")
	public String loginRegister(HttpServletRequest request,HttpServletResponse response,Model model) {
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		if(username.equals("")) {
			model.addAttribute("message","Please input username,Don't Empty!");
			return "show/login";
		}
		if(password.equals("")) {
			model.addAttribute("message","Please input password,Don't Empty!");
			return "show/login";
		}
		Customer customer = new Customer();
		customer.setUsername(username);
		customer.setPassword(password);
		int tag = customerService.loginCustomer(customer);
		
		if(tag == 0) {
			model.addAttribute("message","Please input Correct username And Password");
			return "show/login";
		} else if(tag == 1) {
			model.addAttribute("message","Please active your account ");
			return "show/login";
		} else {
			model.addAttribute("message","Welcome to Our Online Restaurant");
			HttpSession session = request.getSession();
			session.setAttribute("username", customer.getUsername());
			return "show/login";
		}
	}
	
	@RequestMapping("/exitCustomer")
	public String exitCustomer(HttpServletRequest request,HttpServletResponse response) {
		HttpSession session = request.getSession();
		session.removeAttribute("username");
		return "show/login";
	}
	
}
